package com.hxs.learn.action.core;

import java.util.List;

import com.hxs.learn.common.entity.TreeJson;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.common.web.annotation.Login;
import com.hxs.learn.constants.core.web.ReqConst;
import com.hxs.learn.constants.core.web.view.ViewNames;
import com.hxs.learn.service.core.SysRoleMenuPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author kevin
 * @version 1.0
 * @className RoleMenuPermissionController
 * @description 角色菜单权限控制器
 * @date 2018/3/30 16:49
 */
@RestController
@RequestMapping("core/role/menu/permission")
public class RoleMenuPermissionAction extends AbstractViewAction {
    @Autowired
    private SysRoleMenuPermissionService roleMenuPermissionService;

    @ApiOperation(
        value     = "修改角色菜单权限数据",
        notes     = "角色菜单管理时显示修改角色菜单权限数据"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "roleId",
        value     = "角色ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , @ApiImplicitParam(
        name      = "menuId",
        value     = "菜单ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , @ApiImplicitParam(
        name      = "permissionId",
        value     = "权限ID(多个以,拼接)",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) })
    @PostMapping(ReqConst.REQ_MODIFY_DATA)
    @ResponseBody
    public Result modifyRoleMenuData(@Login LoginInfo userInfo, String roleId, String menuId, String permissionId) {
        return roleMenuPermissionService.saveRoleMenuPermission(userInfo, roleId, menuId, permissionId);
    }

    @ApiOperation(
        value     = "编辑角色菜单权限页面",
        notes     = "角色管理时显示编辑角色菜单权限页面"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "roleId",
        value     = "角色ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , @ApiImplicitParam(
        name      = "menuId",
        value     = "菜单ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , @ApiImplicitParam(
        name      = "menuUrl",
        value     = "菜单URL",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , })
    @GetMapping(ReqConst.REQ_MODIFY_ROLE_MENU_PERMISSION_DIALOG)
    public ModelAndView modifyRoleMenuPermissionDialog(Model model, String roleId, String menuId, String menuUrl) {
        model.addAttribute(ReqConst.PARAM_ROLE_ID, roleId);
        model.addAttribute(ReqConst.PARAM_MENU_ID, menuId);
        model.addAttribute(ReqConst.PARAM_MENU_URL, menuUrl);

        return createModelAndView(ViewNames.PAGE_ROLE_MENU_PERMISSION_MODIFY);
    }

    @ApiOperation(
        value     = "加载角色菜单权限数据",
        notes     = "角色菜单管理时显示加载角色菜单权限数据"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "roleId",
        value     = "角色ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , @ApiImplicitParam(
        name      = "menuId",
        value     = "菜单ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , })
    @GetMapping("getRoleMenuPermissionData")
    @ResponseBody
    public Result getRoleMenuPermissionData(String roleId, String menuId) {
        List<TreeJson<Object>> roleMenuPermissionTreeJson =
            roleMenuPermissionService.getRoleMenuPermissionTreeJson(roleId,
                                                                    menuId);

        return Result.ok(roleMenuPermissionTreeJson);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
